< previous page page_476 next page >

Page 476
0476-01.jpg
Figure 21.2
When your program is running from an unknown
location, such as a mapped network drive or CD-ROM,
use 
App.Path to locate the necessary supporting files.
For the program to always find the images, no matter what the CD-ROM drive letter, it uses the Path property of the App object (App.Path), as in the following example:
'Open database
Set db = OpenDatabase(App.Path & "\albums.mdb")

'Load image
Set picture1 = LoadPicture(App.Path & "\Picture1.GIF")
App.Path returns the directory in which the application is running. In the preceding example, the returned value is D:\TUNES or whatever directory the TUNES.EXE file is located in.
note.gif After you have saved your project and are working in the Visual Basic IDE, App.Path returns the path to your project's directory (with the .VBP file). If the project hasn't yet been saved, App.Path returns the current directory. square.gif

In the preceding examples, notice that you have to add a backslash before specifying the filename. App.Path doesn't add the final backslash unless it returns the root directory. You can use the Right$ function to check for this at the beginning of your program:
'Sets sAppPath (assumed to be a public variable) for use later in the program
sAppPath = App.Path
If Right$(sAppPath,1) <> "\" then sAppPath = sAppPath & "\"

'Later  in  the program:
Dim sDBLocation As String
sDBLocation = sAppPath & "finance.mdb"
Some programmers use the ChDir command (discussed earlier) to change the current directory to App.Path at the beginning of the program. I tend to stay away from this command because it also may affect other Windows programs.
tip.gif
If your application uses many files spread across several directories and drives, you should take the next step and keep file locations in an INI file or database. (See "Understanding INI Files" later in the chapter.)

 
< previous page page_476 next page >